package canglang.monitor.cases.dao;

import canglang.monitor.core.base.dao.BaseDAO;
import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.cases.model.CaseLightModel;
import canglang.monitor.user.model.UserModel;

import java.util.Map;
import java.util.HashMap;
import java.util.List;

@SuppressWarnings("unchecked")
public class CaseLightDAO extends BaseDAO {

    public Integer addCaseLight(CaseLightModel caseLightModel) {
        String sql = "addCaseLight";
        Map map = objectToMap(caseLightModel);
        return (Integer) getSqlMapClientTemplate().insert(sql, map);
    }

    public CaseLightModel getCaseLight(int recordId) {
        String sql = "getCaseLight";
        Map map = new HashMap();
        map.put("recordId", recordId);
        return (CaseLightModel) getSqlMapClientTemplate().queryForObject(sql, map);
    }

    public CaseLightModel saveCaseLight(CaseLightModel caseLightModel) {
        String sql = "saveCaseLight";
        Map map = objectToMap(caseLightModel);
        getSqlMapClientTemplate().update(sql, map);
        return this.getCaseLight(caseLightModel.getRecordId());
    }

    public void deleteCaseLight(int recordId) {
        String sql = "deleteCaseLight";
        Map map = new HashMap();
        map.put("recordId", recordId);
        getSqlMapClientTemplate().delete(sql, map);
    }

    public List getWhiteLightList(UserModel user) {
        return this.getActiveLightList("white",user);
    }

    public List getBlueLightList(UserModel user) {
        return this.getActiveLightList("blue",user);
    }

    public List getPurpleLightList(UserModel user) {
        return this.getActiveLightList("purple",user);
    }

    public List getYellowLightList(UserModel user) {
        return this.getActiveLightList("yellow",user);
    }

    public List getRedLightList(UserModel user) {
        return this.getActiveLightList("red",user);
    }

    private List getActiveLightList(String lightType,UserModel user) {
        List list;
        Map map = new HashMap();
        if(user==null){
        	map.put("deptId", 0);
        } else{
        	if("N".equalsIgnoreCase(user.getViewAllInd())){
            	map.put("deptId", user.getDeptId());
            }else{
            	map.put("deptId", 0);
            }	
        }
        String sql = "getActiveCaseLightList";
        if ("white".equalsIgnoreCase(lightType)) {
            map.put("caseType", "06");
        } else if ("blue".equalsIgnoreCase(lightType)) {
            map.put("caseType", "05");
        } else if ("purple".equalsIgnoreCase(lightType)) {
            map.put("caseType", "'02','04'");
        } else if ("yellow".equalsIgnoreCase(lightType)) {
            map.put("caseType", "03");
        } else if ("red".equalsIgnoreCase(lightType)) {
            map.put("caseType", "01");
        }
        list = getSqlMapClientTemplate().queryForList(sql, map);
        return list;
    }

    public void updateCaseFivePplLight(int caseId, String pplInd, String updateId) {
        Map map = new HashMap();
        map.put("caseId", caseId);
        map.put("pplInd", pplInd);
        map.put("updateId", updateId);
        map.put("updateDt", DateUtil.getDate());
        String sql = "updateCaseFivePplLight";
        getSqlMapClientTemplate().update(sql, map);
    }

    public void updateCaseFiveProcLight(int caseId, String procInd, String updateId) {
        Map map = new HashMap();
        map.put("caseId", caseId);
        map.put("procInd", procInd);
        map.put("updateId", updateId);
        map.put("updateDt", DateUtil.getDate());
        String sql = "updateCaseFiveProcLight";
        getSqlMapClientTemplate().update(sql, map);
    }

    public void removeLight(CaseLightModel caseLightModel) {
        if(caseLightModel.getCaseType().equalsIgnoreCase("02")||caseLightModel.getCaseType().equalsIgnoreCase("04")){
            Map map = new HashMap();
            map.put("deptId", caseLightModel.getDeptId());
            map.put("caseType", "02");
            map.put("lightInd", "Y");
            String sql = "removeLight";
            getSqlMapClientTemplate().update(sql, map);
            map.put("caseType", "04");
            getSqlMapClientTemplate().update(sql, map);
        }else{
            Map map = new HashMap();
            map.put("deptId", caseLightModel.getDeptId());
            map.put("caseType", caseLightModel.getCaseType());
            map.put("lightInd", "Y");
            String sql = "removeLight";
            getSqlMapClientTemplate().update(sql, map);
        }

    }

    public void updateLightDept(String caseType,int caseId,int deptId){
        Map map = new HashMap();
        map.put("caseId", caseId);
        map.put("deptId", deptId);
        map.put("caseType", caseType);
        String sql = "updateLightDept";
        getSqlMapClientTemplate().update(sql, map);
    }

    public void deleteUnuseLight(String caseType,int caseId){
        Map map = new HashMap();
        map.put("caseId", caseId);
        map.put("caseType", caseType);
        String sql = "deleteUnuseLight";
        getSqlMapClientTemplate().delete(sql, map);
    }
}
